<?php

namespace App\Admin\Controllers;

use App\Admin\Controllers\Helpers\LinkHelper;
use App\Models\UserWalletActive;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UserWalletActiveController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserWalletActive(), function (Grid $grid) {
            $grid->model()->with(['user']);
            $grid->quickSearch('id', 'user_id','user.uuid','user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;

            $grid->model()->orderBy('created_at', 'desc');

            $grid->column('id')->sortable();
            $grid->column('user_id')
                ->display(function () {
                    return LinkHelper::displayUser($this->user);
                });
            $grid->column('user_withdraw_id')
                ->display(function () {
                    $withdrawId = $this->user_withdraw_id;
                    $text = '<i>-</i>';
                    if ($withdrawId) {
                        $text = "<a target='_blank' href='user-withdraws?id={$withdrawId}'>{$withdrawId}</a>";
                    }
                    return $text;
                });
            $grid->column('wallet_type')
                ->using([
                    'reward' => '广告收益',
                    'commission' => '佣金收益',
                    'task' => '任务收益',
                ], '未知')->label([
                    'reward' => 'default',
                    'commission' => 'info',
                ]);
            $grid->column('action')
                ->using([
                    'income' => '收入',
                    'outcome' => '提现',
                ], '未知')->label([
                    'income' => 'success',
                    'outcome' => 'danger',
                ]);
            $grid->column('coins')->sortable();
            $grid->column('before_reward_coins')->sortable();
            $grid->column('after_reward_coins')->sortable();
//            $grid->column('before_commission_coins')->sortable();
//            $grid->column('after_commission_coins')->sortable();
            $grid->column('status')
                ->using([
                    'pending' => '处理中',
                    'reject' => '已拒绝',
                    'approved' => '已通过',
                ], '未知')->label([
                    'pending' => 'info',
                    'reject' => 'danger',
                    'approved' => 'success',
                ]);
            $grid->column('changed_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

                $filter->equal('user_id');
                $filter->equal('action')->radio([
                    'income' => '收入',
                    'outcome' => '提现',
                ]);
                $filter->equal('wallet_type')->radio([
                    'reward' => '广告收益',
                    'commission' => '佣金收益',
                ]);
            });

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableBatchDelete();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserWalletActive(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('user_withdraw_id');
            $show->field('operator_id');
            $show->field('wallet_type');
            $show->field('action');
            $show->field('coins');
            $show->field('before_reward_coins');
            $show->field('after_reward_coins');
            $show->field('before_commission_coins');
            $show->field('after_commission_coins');
            $show->field('status');
            $show->field('changed_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserWalletActive(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('user_withdraw_id');
            $form->text('operator_id');
            $form->text('wallet_type');
            $form->text('action');
            $form->text('coins');
            $form->text('before_reward_coins');
            $form->text('after_reward_coins');
            $form->text('before_commission_coins');
            $form->text('after_commission_coins');
            $form->text('status');
            $form->text('changed_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
